import 'package:flutter/material.dart';

class MyContainer extends StatelessWidget {
  /// 必须传入的子组件
  final Widget? child;

  /// 点击事件回调，可选
  final VoidCallback? onTap;

  /// 是否启用水波纹效果，默认启用
  final bool enableRipple;

  /// Container 自身所有参数，透传
  final Color? color;
  final double? width;
  final double? height;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;
  final Decoration? decoration;
  final AlignmentGeometry? alignment;

  const MyContainer({
    Key? key,
    this.child,
    this.onTap,
    this.enableRipple = true,
    this.color,
    this.width,
    this.height,
    this.padding,
    this.margin,
    this.decoration,
    this.alignment,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget container = Container(
      color: color,
      width: width,
      height: height,
      padding: padding,
      margin: margin,
      decoration: decoration,
      alignment: alignment,
      child: child,
    );

    // 如果没传点击事件，直接返回Container
    if (onTap == null) {
      return container;
    }

    // 如果启用水波纹，使用InkWell包裹
    if (enableRipple) {
      return Material(
        color: Colors.transparent, // 透明材质，显示Container背景色
        child: InkWell(
          // behavior设置为opaque，让整个区域都响应点击
          // behavior: HitTestBehavior.opaque,
          onTap: onTap,
          child: container,
        ),
      );
    }

    // 不启用水波纹，用GestureDetector包裹，behavior translucent使空白区域可点
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: container,
    );
  }
}
